using CSharpWebService.Services;
using Microsoft.AspNetCore.Mvc;

namespace CSharpWebService.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class HttpController : ControllerBase
    {
        private readonly HttpService _httpService;

        public HttpController(HttpService httpService)
        {
            _httpService = httpService;
        }

        [HttpGet("baidu")]
        public async Task<IActionResult> GetBaidu()
        {
            try
            {
                // 调用C语言库请求百度首页
                var result = await _httpService.GetAsync("http://www.baidu.com/");
                
                // 返回HTML内容给浏览器
                return Content(result, "text/html; charset=utf-8");
            }
            catch (Exception ex)
            {
                return BadRequest($"请求失败: {ex.Message}");
            }
        }

        [HttpGet("request")]
        public async Task<IActionResult> GetUrl([FromQuery] string url)
        {
            if (string.IsNullOrEmpty(url))
            {
                return BadRequest("URL参数不能为空");
            }

            try
            {
                var result = await _httpService.GetAsync(url);
                return Content(result, "text/html; charset=utf-8");
            }
            catch (Exception ex)
            {
                return BadRequest($"请求失败: {ex.Message}");
            }
        }

        [HttpGet]
        public IActionResult Get()
        {
            var html = @"
<!DOCTYPE html>
<html>
<head>
    <meta charset='UTF-8'>
    <title>C语言HTTP请求库测试</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        .container { max-width: 800px; margin: 0 auto; }
        .button { background: #007bff; color: white; padding: 10px 20px; border: none; border-radius: 4px; cursor: pointer; margin: 5px; }
        .button:hover { background: #0056b3; }
        .result { margin-top: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 4px; background: #f9f9f9; }
        input[type='text'] { padding: 8px; width: 300px; margin-right: 10px; }
    </style>
</head>
<body>
    <div class='container'>
        <h1>C语言HTTP请求库测试页面</h1>
        <p>这个页面通过C# Web服务调用C语言HTTP请求库</p>
        
        <div>
            <button class='button' onclick='testBaidu()'>测试百度首页</button>
            <br><br>
            <input type='text' id='customUrl' placeholder='输入URL地址' value='http://www.baidu.com/'>
            <button class='button' onclick='testCustomUrl()'>测试自定义URL</button>
        </div>
        
        <div id='result' class='result' style='display:none;'>
            <h3>请求结果:</h3>
            <iframe id='resultFrame' style='width:100%; height:400px; border:1px solid #ccc;'></iframe>
        </div>

        <script>
            function testBaidu() {
                fetch('/http/baidu')
                    .then(response => response.text())
                    .then(html => {
                        document.getElementById('result').style.display = 'block';
                        var frame = document.getElementById('resultFrame');
                        frame.srcdoc = html;
                    })
                    .catch(error => {
                        alert('请求失败: ' + error);
                    });
            }

            function testCustomUrl() {
                var url = document.getElementById('customUrl').value;
                if (!url) {
                    alert('请输入URL地址');
                    return;
                }
                
                fetch('/http/request?url=' + encodeURIComponent(url))
                    .then(response => response.text())
                    .then(html => {
                        document.getElementById('result').style.display = 'block';
                        var frame = document.getElementById('resultFrame');
                        frame.srcdoc = html;
                    })
                    .catch(error => {
                        alert('请求失败: ' + error);
                    });
            }
        </script>
    </div>
</body>
</html>";
            return Content(html, "text/html; charset=utf-8");
        }
    }
}
